import { isAuth } from "@/utils";
import { message } from "antd";
import { useEffect } from "react";
import { Redirect, Route, RouteProps } from "react-router-dom";

function AuthRoute(props: RouteProps) {
  const { children, ...rest } = props;

useEffect(()=>{
  if(!isAuth()){
    //触发了message组件
    message.warning("请先登录");
  }
})
  return (
    <Route
      {...rest}
      render={({ location }) => {
        //判断是否登录
        if (isAuth()) {
          return children;
        } else {
          return (
            <Redirect to={{ pathname: "/login", state: { from: location } }} />
          );
        }
      }}
    />
  );
}

export default AuthRoute;
